package com.shangcheng.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.*;
import com.shangcheng.model.domain.*;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.PmsProductVo;
import com.shangcheng.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PmsProductService {
    @DubboReference
    private PmsProductApi productApi;

    public PageResult listByPage(Integer brandId, String keyword, Integer pageNum, Integer pageSize, String productSn, Integer productCategoryId, Integer verifyStatus, Integer publishStatus) {
        IPage<PmsProduct> pg = productApi.listByPage(brandId, keyword, pageNum, pageSize, productSn, productCategoryId, verifyStatus, publishStatus);
        PageResult result = new PageResult();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(pg.getRecords());
        result.setTotal(Convert.toInt(pg.getTotal()));
        result.setTotalPage(Convert.toInt(pg.getPages()));
        return result;
    }

    public List<PmsProduct> simpleList(String keyword) {
        return productApi.simpleList(keyword);
    }

    public void updateDeleteStatus(Integer deleteStatus, List<Integer> ids) {
        productApi.updateDeleteStatus(deleteStatus, ids);
    }

    public void updateNewStatus(Integer newStatus, List<Integer> ids) {
        productApi.updateNewStatus(newStatus, ids);
    }

    public void updatePublishStatus(Integer publishStatus, List<Integer> ids) {
        productApi.updatePublishStatus(publishStatus, ids);
    }

    public void updateReocmmandStatus(Integer recommendStatus, List<Integer> ids) {
        productApi.updateReocmmandStatus(recommendStatus, ids);
    }

    public void updateVerifyStatus(Integer verifyStatus, List<Integer> ids) {
        productApi.updateVerifyStatus(verifyStatus, ids);
    }

    @DubboReference
    private PmsMemberPriceApi memberPriceApi;

    @DubboReference
    private PmsProductFullReductionApi productFullReductionApi;

    @DubboReference
    private PmsProductLadderApi productLadderApi;

    @DubboReference
    private CmsSubjectProductRelationApi subjectProductRelationApi;

    @DubboReference
    private CmsPrefrenceAreaProductRelationApi prefrenceAreaProductRelationApi;

    @DubboReference
    private PmsProductAttributeValueApi productAttributeValueApi;

    @DubboReference
    private PmsSkuStockApi skuStockApi;

    public void create(PmsProductVo productVo) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(productVo, pmsProduct);
        PmsProduct product = null;
        try {
            product = productApi.create(pmsProduct);
        } catch (Exception e) {
            throw new BusinessException("商品货号已存在");
        }
        //保存会员商品关系
        List<PmsMemberPrice> memberPriceList = productVo.getMemberPriceList();
        memberPriceApi.save(memberPriceList, product.getId());
        //保存商品满减关系
        List<PmsProductFullReduction> productFullReductionList = productVo.getProductFullReductionList();
        productFullReductionApi.save(productFullReductionList, product.getId());
        //保存商品折扣关系
        List<PmsProductLadder> productLadderList = productVo.getProductLadderList();
        productLadderApi.save(productLadderList, product.getId());
        //保存关联专题和商品关系
        List<CmsSubjectProductRelation> subjectProductRelationList = productVo.getSubjectProductRelationList();
        subjectProductRelationApi.save(subjectProductRelationList, product.getId());
        //保存关联优选和商品关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = productVo.getPrefrenceAreaProductRelationList();
        prefrenceAreaProductRelationApi.save(prefrenceAreaProductRelationList, product.getId());
        //保存商品参数
        List<PmsProductAttributeValue> productAttributeValueList = productVo.getProductAttributeValueList();
        productAttributeValueApi.save(productAttributeValueList, product.getId());
        //保存商品规格
        List<PmsSkuStock> skuStockList = productVo.getSkuStockList();
        skuStockApi.save(skuStockList, product.getId());
    }

    public PmsProductVo updateInfoById(Integer id) {
        PmsProductVo vo = new PmsProductVo();
        PmsProduct product = productApi.findById(id);
        //查询会员商品关系
        List<PmsMemberPrice> memberPriceList = memberPriceApi.findByProductId(id);
        //查询商品满减关系
        List<PmsProductFullReduction> productFullReductionList = productFullReductionApi.findByProductId(id);
        //查询商品折扣关系
        List<PmsProductLadder> productLadderList = productLadderApi.findByProductId(id);
        //查询关联专题和商品关系
        List<CmsSubjectProductRelation> subjectProductRelationList = subjectProductRelationApi.findByProductId(id);
        //查询关联优选和商品关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = prefrenceAreaProductRelationApi.findByProductId(id);
        //查询商品参数
        List<PmsProductAttributeValue> productAttributeValueList = productAttributeValueApi.findByProductId(id);
        //查询商品规格
        List<PmsSkuStock> skuStockList = skuStockApi.findByProductId(id);

        BeanUtil.copyProperties(product, vo);
        vo.setMemberPriceList(memberPriceList);
        vo.setPrefrenceAreaProductRelationList(prefrenceAreaProductRelationList);
        vo.setProductAttributeValueList(productAttributeValueList);
        vo.setProductLadderList(productLadderList);
        vo.setProductFullReductionList(productFullReductionList);
        vo.setSubjectProductRelationList(subjectProductRelationList);
        vo.setSkuStockList(skuStockList);
        return vo;
    }

    public void updateById(Integer id, PmsProductVo productVo) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtil.copyProperties(productVo, pmsProduct);
        //更新商品信息
        String product = productApi.updateById(pmsProduct);
        if (!StrUtil.equals("ok", product)) {
            throw new BusinessException(product);
        }
        //更新会员商品关系
        memberPriceApi.updateById(productVo.getMemberPriceList(), id);
        //更新商品满减关系
        productFullReductionApi.updateById(productVo.getProductFullReductionList(),id);
        //更新商品折扣关系
        productLadderApi.updateById(productVo.getProductLadderList());
        //更新关联专题和商品关系
        subjectProductRelationApi.updateById(id, productVo.getSubjectProductRelationList());
        //更新关联优选和商品关系
        prefrenceAreaProductRelationApi.updateById(id, productVo.getPrefrenceAreaProductRelationList());
        //更新商品参数
        productAttributeValueApi.updateById(id, productVo.getProductAttributeValueList());
        //更新商品规格
        skuStockApi.updateById(id, productVo.getSkuStockList());
    }
}
